#include "U2DemoApp.h"

#include "U2Log.h"
#include "U2Framework.h"
#include "U2RenderWindow.h"


DemoApp::DemoApp()
{
}

//|||||||||||||||||||||||||||||||||||||||||||||||

DemoApp::~DemoApp()
{
    delete U2Framework::getSingletonPtr();
}

void DemoApp::startDemo()
{
	new U2Framework();
    /**********************@@@@@@@@@@@@@@@@@@@@@
	if(!U2Framework::getSingletonPtr()->initOgre("DemoApp v1.0", this, 0))
		return;
     */
    if(!U2Framework::getSingletonPtr()->initOgre("DemoApp v1.0"))
		return;
    
	m_bShutdown = false;
    
	U2Framework::getSingletonPtr()->m_pLog->logMessage("Demo initialized!");
    
	setupDemoScene();
#if !((U2_PLATFORM == U2_PLATFORM_APPLE) && __LP64__)
	runDemo();
#endif
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void DemoApp::setupDemoScene()
{
    /**********************@@@@@@@@@@@@@@@@@@@@@
	U2Framework::getSingletonPtr()->m_pSceneMgr->setSkyBox(true, "Examples/SpaceSkyBox");
    
	U2Framework::getSingletonPtr()->m_pSceneMgr->createLight("Light")->setPosition(75,75,75);
    
	m_pCubeEntity = U2Framework::getSingletonPtr()->m_pSceneMgr->createEntity("Cube", "ogrehead.mesh");
	m_pCubeNode = U2Framework::getSingletonPtr()->m_pSceneMgr->getRootSceneNode()->createChildSceneNode("CubeNode");
	m_pCubeNode->attachObject(m_pCubeEntity);
     */
}

//|||||||||||||||||||||||||||||||||||||||||||||||

void DemoApp::runDemo()
{
	U2Framework::getSingletonPtr()->m_pLog->logMessage("Start main loop...");
	
	double timeSinceLastFrame = 0;
	double startTime = 0;
    
    U2Framework::getSingletonPtr()->m_pRenderWnd->resetStatistics();
    
#if (!defined(U2_IS_IOS)) && !((U2_PLATFORM == U2_PLATFORM_APPLE) && __LP64__)
	while(!m_bShutdown && !U2Framework::getSingletonPtr()->isOgreToBeShutDown()) 
	{
		if(U2Framework::getSingletonPtr()->m_pRenderWnd->isClosed())m_bShutdown = true;
        
#if U2_PLATFORM == U2_PLATFORM_WIN32 || U2_PLATFORM == U2_PLATFORM_LINUX || U2_PLATFORM == U2_PLATFORM_APPLE
		U2EG_NAMESPACE_NAME::U2WindowEventUtilities::messagePump();
#endif	
		if(U2Framework::getSingletonPtr()->m_pRenderWnd->isActive())
		{
			startTime = U2Framework::getSingletonPtr()->m_pTimer->getMillisecondsCPU();
            
#if !U2_IS_IOS
			U2Framework::getSingletonPtr()->m_pKeyboard->capture();
#endif
			U2Framework::getSingletonPtr()->m_pMouse->capture();
            
			U2Framework::getSingletonPtr()->updateOgre(timeSinceLastFrame);
			U2Framework::getSingletonPtr()->m_pRoot->renderOneFrame();
            
			timeSinceLastFrame = U2Framework::getSingletonPtr()->m_pTimer->getMillisecondsCPU() - startTime;
		}
		else
		{
#if U2_PLATFORM == U2_PLATFORM_WIN32
            Sleep(1000);
#else
            sleep(1);
#endif
		}
	}
#endif
    
#if !defined(U2_IS_IOS)
	U2Framework::getSingletonPtr()->m_pLog->logMessage("Main loop quit");
	U2Framework::getSingletonPtr()->m_pLog->logMessage("Shutdown OGRE...");
#endif
}

/**********************@@@@@@@@@@@@@@@@@@@@@
//|||||||||||||||||||||||||||||||||||||||||||||||

bool DemoApp::keyPressed(const OIS::KeyEvent &keyEventRef)
{
#if !defined(U2_IS_IOS)
	U2Framework::getSingletonPtr()->keyPressed(keyEventRef);
#endif
	return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool DemoApp::keyReleased(const OIS::KeyEvent &keyEventRef)
{
#if !defined(U2_IS_IOS)
	U2Framework::getSingletonPtr()->keyReleased(keyEventRef);
#endif

	return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||
*/